package com.cisdi.blog.dao;

import com.cisdi.blog.entity.Blog;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 博客
 * @author pengyangyan
 * @version 1.0.0
 * @date 2020年03月18日 09:44:00
 */
@Repository
public interface BlogRepository extends JpaRepository<Blog,Long>, JpaSpecificationExecutor<Blog> {

    @Query("select b from Blog b where b.recommend = true")
    List<Blog> findTop(Pageable pageable);

    @Transactional
    @Modifying
    @Query("update Blog b set b.views = ?2 where b.id = ?1")
    int updateViews(Long id,Integer views);

    /**
     * SELECT DATE_FORMAT(b.update_time,'%Y') as years FROM `t_blog` b GROUP BY years ORDER BY years desc
     */
    @Query("select function('DATE_FORMAT',b.updateTime,'%Y') as years from Blog b group by years order by years desc ")
    List<String> getYears();

    @Query("select b from Blog b where function('DATE_FORMAT',b.updateTime,'%Y') = ?1")
    List<Blog> getBlogByYears(String years);

    @Query("select b from Blog b where b.title like ?1 or b.content like ?1")
    Page<Blog> findByQuery(String query,Pageable pageable);



}
